package com.ilink.groundservice.controller.AiX;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ilink.groundservice.entity.VO.AiX.AiXCourseResponse;
import com.ilink.groundservice.entity.VO.AiX.AiXResultResponse;
import com.ilink.ilinkapi.dto.ground.AiX.AiXAreaFilterRequest;
import com.ilink.ilinkapi.dto.ground.AiX.AiXResultInsertRequest;
import com.ilink.ilinkapi.dto.ground.AiX.AiXResultUpdateRequest;
import com.ilink.groundservice.service.AiX.AiXResultService;
import com.ilink.ilinkcommon.domain.R;
import com.ilink.ilinkcommon.domain.User;
import com.ilink.ilinkcommon.log.method.MethodLog;
import com.ilink.ilinkcommon.utils.BeanUtils;
import com.ilink.ilinkcommon.utils.UserContext;
import com.ilink.ilinkapi.client.UserServiceClient;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;

@Slf4j
@RequestMapping("/ground/aix/result")
@RestController
@RequiredArgsConstructor
public class AiXResultController {

  private final AiXResultService aiXResultService;
  private final UserServiceClient userServiceClient;

  @MethodLog("分页返回结果元数据")
  @GetMapping("/get_results")
  public R getCourses(AiXAreaFilterRequest request) {
    try {
      IPage<AiXResultResponse> responseIPage = aiXResultService.getResults(request);
      if (responseIPage != null) {
        return R.ok("分页返回结果元数据", responseIPage);
      } else {
        return R.error("查询结果数据失败");
      }
    } catch (Exception e) {
      log.error(e.getMessage());
      return R.error(e.getMessage());
    }
  }

  @MethodLog("插入结果元数据")
  @PostMapping("/insert_result")
  public R insertResult(AiXResultInsertRequest request) {
    if (!isTeacher(UserContext.getUserId())) {
      return R.error("无教师权限");
    }
    try {
      aiXResultService.insertResult(request);
      return R.ok("结果插入成功");
    } catch (Exception e) {
      log.error(e.getMessage());
      return R.error(e.getMessage());
    }
  }

  @MethodLog("更新结果元数据")
  @PostMapping("/update_result")
  public R updateResult(@RequestBody AiXResultUpdateRequest request) {
    if (!isTeacher(UserContext.getUserId())) {
      return R.error("无教师权限");
    }
    try {
      aiXResultService.updateResult(request);
      return R.ok("结果更新成功");
    } catch (Exception e) {
      log.error(e.getMessage());
      return R.error(e.getMessage());
    }
  }

  @MethodLog("更新结果封面图")
  @PostMapping("/update_result_cover")
  public R updateResultCover(MultipartFile file, Long resultId, Long areaId) {
    if (!isTeacher(UserContext.getUserId())) {
      return R.error("无教师权限");
    }
    try {
      R Result = aiXResultService.updateResultCover(file, resultId, areaId);
      return Result;
    } catch (Exception e) {
      log.error(e.getMessage());
      return R.error(e.getMessage());
    }
  }

  @MethodLog("删除结果元数据")
  @PostMapping("/delete_result/{resultId}")
  public R deleteResult(@PathVariable Long resultId) {
    if (!isTeacher(UserContext.getUserId())) {
      return R.error("无教师权限");
    }
    try {
      aiXResultService.deleteResult(resultId);
      return R.ok("结果删除成功");
    } catch (Exception e) {
      log.error(e.getMessage());
      return R.error(e.getMessage());
    }
  }

  @MethodLog("获取单个结果")
  @GetMapping("/get_result/{resultId}")
  public R getResult(@PathVariable Long resultId) {
    try {
      AiXResultResponse response = aiXResultService.getResult(resultId);
      return R.ok("结果获取成功", response);
    } catch (Exception e) {
      log.error(e.getMessage());
      return R.error(e.getMessage());
    }
  }

  private boolean isTeacher(String userId) {
    R whoami = userServiceClient.whoami(userId);
    HttpStatus status = HttpStatus.valueOf(whoami.getCode());
    if (!status.is2xxSuccessful()) {
      log.error(String.format("获取用户信息失败，用户ID: %s", userId));
      return false;
    }
    User userDetail = BeanUtils.copyBean(whoami.getData(), User.class);
    String userType = userDetail.getUserType();
    return Arrays.asList("BIAdmin", "L0101", "L0107", "L0108", "L0109").contains(userType);
  }
}
